//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>

static Aura_ECode threadEntry(Aura_PVoid pArgs)
{
    Aura_ThreadId threadId = INVALID_TID;
    AuraThread_GetCurrentId(&threadId);
    Aura_printf(" <child> : thread id = %d\n", threadId);

    return NOERROR;
}

Boolean AuraTest_Join_After_thread_Death()
{
    Aura_ThreadId threadId = INVALID_TID;
    Aura_PVoid pPrivateData = NULL;
    Aura_ECode ec = NOERROR;

    ec = AuraThread_Create(threadEntry, NULL, 0, &threadId, &pPrivateData);
    if (AURA_FAILED(ec)) {
        Aura_printf(" father: <AuraThread_Create> failed. (ec = 0x%x)\n", ec);
        goto ErrorExit;
    }
    Aura_printf(" father: new threadId id = %d\n", threadId);

    Aura_WaitResult wr;
    AuraThread_Sleep(1000, &wr);

    ec = AuraThread_Join(threadId, AURA_INFINITE, &wr);
    if (AURA_FAILED(ec) || (Aura_WaitResult_OK != wr)) {
        Aura_printf(" father: <AuraThread_Join> failed. (ec = 0x%x)\n", ec);
        goto ErrorExit;
    }

    QuitCode quitCode;
    ec = AuraThread_GetQuitCode(threadId, &quitCode);
    if (AURA_FAILED(ec) || AURA_FAILED(quitCode)) {
        Aura_printf(" father: <AuraThread_GetQuitCode> failed. (ec = 0x%x)\n", ec);
        goto ErrorExit;
    }

    AuraThread_DestroyPrivateData(threadId, pPrivateData);

    return TRUE;

ErrorExit:
    if (NULL != pPrivateData) {
        AuraThread_DestroyPrivateData(threadId, pPrivateData);
        pPrivateData = NULL;
    }
    return AURA_FALSE;
}

